//
//  CheckPeopleCell.swift
//  Employee
//
//  Created by swordjoy on 2017/6/26.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

protocol CheckPeopleCellDelegate {
    func getApprovalStaffId(ids: String, selectedIndex: [Int])
}

class CheckPeopleCell: UITableViewCell {

  static let identifier = "CheckPeopleCell.swordjoy"
  static func height(with models: [CheckPeople]) -> CGFloat {
    return (80 + 25 * (models.count * 2 - 1 )).cgFloat
  }
  
  var delegate: CheckPeopleCellDelegate?
  
//  var models: [CheckPeople] = [] {
//    didSet {
//      hotLayout()
//    }
//  }
    var model: FlowDetail? {
        didSet {
            guard let temp = model else {
                return
            }
            hotLayout(model: temp)
        }
    }
    
    var selectedIndex: [Int] = []
  fileprivate var containerView: UIView!
  fileprivate var titleLabel: UILabel!
  
  fileprivate var imageViews: [UIImageView] = []
  fileprivate var nameLabels: [UILabel] = []
  fileprivate var changeButtons: [UIButton] = []
  
  // 审核人数组
  fileprivate var ids: [String] = []
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    selectionStyle = .none
    backgroundColor = UIColor.white
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
}

// MARK: - action

extension CheckPeopleCell {
  func changeAction(_ button: UIButton) {
    let tag = button.tag
    
    let sameLevelPicker = SJSameLevelPicker()
    
    var titles: [String] = []
    for item in model!.checkPeoples[tag].roles {
      titles.append(item.roleStaffName)
    }
    sameLevelPicker.titless = [titles]
    
    sameLevelPicker.title = "请选择"
    sameLevelPicker.leftTitle = "取消"
    
    sameLevelPicker.didSelectClosure = { [unowned self] (title, index) in
      let temp = self.model!.checkPeoples[tag].roles[index]
      self.imageViews[tag].setImage(with: temp.roleStaffAvatar, placeholder: GlobalConfig.DefaultHeadIconImage)
      self.nameLabels[tag].text = temp.roleStaffName
      self.ids[tag] = temp.roleStaffId
      
      let idsString = self.ids.joined(separator: ",")
      self.selectedIndex[tag] = index
      self.delegate?.getApprovalStaffId(ids: idsString, selectedIndex:self.selectedIndex)
    }
    
    sameLevelPicker.show(in: SJApp.keyWindow)
  }
}

extension CheckPeopleCell {
  
  fileprivate func hotLayout(model: FlowDetail) {
    for imageView in imageViews { imageView.removeFromSuperview() }
    imageViews.removeAll()
    for label in nameLabels { label.removeFromSuperview() }
    nameLabels.removeAll()
    for button in changeButtons { button.removeFromSuperview() }
    changeButtons.removeAll()
    
    ids.removeAll()
    
    for (index, item) in model.checkPeoples.enumerated() {
      
      let iconImageView = UIImageView()
        
      
      contentView.addSubview(iconImageView)
      iconImageView.snp.makeConstraints { (make) in
        make.top.equalTo(60 + index * 50)
        make.left.equalTo(32)
        make.height.width.equalTo(25)
      }
      iconImageView.layer.cornerRadius = 2
      iconImageView.clipsToBounds = true
      
      let nameLabel = UILabel.getLabel(with: GlobalConfig.system16Font, textColor: GlobalConfig.TextDeepColor)
      containerView.addSubview(nameLabel)
      nameLabel.snp.makeConstraints { (make) in
        make.left.equalTo(iconImageView.snp.right).offset(10)
        make.centerY.equalTo(iconImageView)
      }

      let changeButton = UIButton()
      containerView.addSubview(changeButton)
      changeButton.titleLabel?.font = GlobalConfig.system14Font
      changeButton.setTitleColor(GlobalConfig.BGSkyBlueColor, for: .normal)
      changeButton.setTitle("更换", for: .normal)
      changeButton.addTarget(self, action: #selector(changeAction(_:)), for: .touchUpInside)
      changeButton.tag = index
      changeButton.snp.makeConstraints { (make) in
        make.centerY.equalTo(nameLabel)
        make.left.equalTo(nameLabel.snp.right).offset(20)
        make.height.equalTo(25)
        make.width.equalTo(30)
        make.right.equalTo(-10)
      }
        
        if model.flowType == "STAFF" {
            changeButton.isHidden = true
        }
        else {
            if item.roles.count == 1 {
                changeButton.isHidden = true
            }
        }
      
      let lineView = UIView()
      containerView.addSubview(lineView)
      lineView.backgroundColor = GlobalConfig.BGWhiteColor
      lineView.snp.makeConstraints({ (make) in
        make.top.equalTo(90 + index * 50)
        make.width.equalTo(3)
        make.height.equalTo(15)
        make.centerX.equalTo(iconImageView)
      })
        
        
        if model.flowType == "STAFF" {
            iconImageView.setImage(with: item.approvalStaffAvatar, placeholder: GlobalConfig.DefaultHeadIconImage)
            nameLabel.text = item.approvalName
            ids.append(item.approvalStaffId)
        }
        else {
            let tempIndex = selectedIndex[index]
            iconImageView.setImage(with: item.roles[tempIndex].roleStaffAvatar, placeholder: GlobalConfig.DefaultHeadIconImage)
            nameLabel.text = item.roles[tempIndex].roleStaffName
            ids.append(item.roles[tempIndex].roleStaffId)
        }
      
      if index == model.checkPeoples.count - 1 {
        lineView.isHidden =  true
      }
      
      imageViews.append(iconImageView)
      nameLabels.append(nameLabel)
      changeButtons.append(changeButton)
    }
    
    let idsString = ids.joined(separator: ",")
    delegate?.getApprovalStaffId(ids: idsString, selectedIndex: [])
  }
  
  func create() {
    containerView = UIView()
    contentView.addSubview(containerView)
    containerView.backgroundColor = UIColor.white
    containerView.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.top.equalToSuperview()
      make.bottom.equalToSuperview()
    }
    containerView.layer.cornerRadius = 2
    containerView.clipsToBounds = true
    
    titleLabel = UILabel()
    containerView.addSubview(titleLabel)
    titleLabel.textColor = GlobalConfig.TextDeepColor
    titleLabel.font = GlobalConfig.system15Font
    titleLabel.text = "将由以下人员审批"
    titleLabel.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.top.equalTo(20)
    }
  }
  
}
